package fr.chrysalis.ui.components.display
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import fr.chrysalis.ui.UI;
	import fr.chrysalis.ui.assets.IAsset;
	import fr.chrysalis.ui.components.core.Component;
	import fr.chrysalis.ui.components.events.ButtonEvent;
	import fr.chrysalis.ui.components.interfaces.IComponent;
	import fr.chrysalis.utils.Debug;

	public class Button extends Component implements IComponent
	{
		
		/**
		 * Statut du click sur le bouton 
		 */
		protected var isPressed:Boolean;
		
		private var _text:String;
		/**
		 * Récupère le texte affiché dans le bouton 
		 */
		public function get text():String
		{
			return _text;
		}
		/**
		 * Modifie le texte affiché dans le bouton 
		 * @param value Valeur du texte
		 */
		public function set text(value:String):void
		{
			try { this.element.txt1.txt.text = value; } catch(e:Error){ }
			try { this.element.txt2.txt.text = value; } catch(e:Error){ }
			try { this.element.txt3.txt.text = value; } catch(e:Error){ }
		}

		/**
		 * Crée un nouveau bouton 
		 * @param asset Asset à assigner au bouton
		 */
		public function Button(asset:IAsset)
		{
			super(asset);
			this.enable();
			this.isPressed = false;
		}
		
		/**
		 * Active le bouton 
		 */
		override public function enable():void {
			super.enable();
			this.play('enable');
			this.element.area.addEventListener(MouseEvent.ROLL_OVER, handlerMouseRollOver);
			this.element.area.addEventListener(MouseEvent.ROLL_OUT, handlerMouseRollOut);
		}
		
		/**
		 * Désactive le bouton 
		 */
		override public function disable():void {
			super.disable();
			this.play('disable');
			this.element.area.removeEventListener(MouseEvent.ROLL_OVER, handlerMouseRollOver);
			this.element.area.removeEventListener(MouseEvent.ROLL_OUT, handlerMouseRollOut);
		}
		
		/**
		 * Simule un roll over sur le bouton 
		 */
		public function rollOver():void{
			this.play('over');
			this.dispatchEvent(new ButtonEvent(ButtonEvent.ROLL_OVER));
		}
		
		/**
		 * Simule un roll out sur le bouton 
		 */
		public function rollOut():void{
			if(!this.isPressed){
				this.play('over', true, true);
			}else{
				this.play('enable');
				this.element.area.removeEventListener(Event.ENTER_FRAME, dispatchMouseDown);
			}
			this.dispatchEvent(new ButtonEvent(ButtonEvent.ROLL_OUT));
		}
		
		/**
		 * Simule un press sur le bouton 
		 */
		protected function mouseDown():void{
			this.play('click');
			if(!this.isPressed){
				this.dispatchEvent(new ButtonEvent(ButtonEvent.PRESS));
			}
			this.isPressed = true;
			this.element.area.addEventListener(Event.ENTER_FRAME, dispatchMouseDown);
		}
		
		protected function dispatchMouseDown(evt:Event):void {
			this.dispatchEvent(new ButtonEvent(ButtonEvent.MOUSE_DOWN));
		}
		
		/**
		 * Simule un relachement du click sur le bouton 
		 */
		protected function mouseUp():void{
			this.play('enable');
			this.isPressed = false;
			this.dispatchEvent(new ButtonEvent(ButtonEvent.RELEASE));
			this.dispatchEvent(new ButtonEvent(ButtonEvent.CLICK));
			this.element.area.removeEventListener(Event.ENTER_FRAME, dispatchMouseDown);
		}
		
		/**
		 * Simule un click sur le bouton 
		 */
		public function click():void{
			this.mouseDown();
			this.mouseUp();
		}
		
		/**
		 * Handler lors d'un MouseEvent.ROLL_OVER 
		 * @param evt
		 */
		protected function handlerMouseRollOver(evt:MouseEvent):void {
			this.rollOver();
			this.element.area.addEventListener(MouseEvent.MOUSE_DOWN, handlerMouseDown);
		}
		
		/**
		 * Handler lors d'un MouseEvent.ROLL_OUT  
		 * @param evt
		 */
		protected function handlerMouseRollOut(evt:MouseEvent):void {
			this.rollOut();
			this.isPressed = false;
			this.element.area.removeEventListener(MouseEvent.MOUSE_DOWN, handlerMouseDown);
			this.element.area.removeEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
		}
		
		/**
		 * Handler lors d'un MouseEvent.MOUSE_UP
		 * @param evt
		 */
		protected function handlerMouseUp(evt:MouseEvent):void {
			this.mouseUp();
			this.element.area.removeEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
		}
		
		/**
		 * Handler lors d'un MouseEvent.MOUSE_DOWN
		 * @param evt
		 */
		protected function handlerMouseDown(evt:MouseEvent):void {
			this.mouseDown();
			this.element.area.addEventListener(MouseEvent.MOUSE_UP, handlerMouseUp);
		}
		
				
	}
}